
package com.ifcar.app.fragment;

import com.ifcar.app.util.GroupAdapter;
import com.ifcar.app.util.GroupListView;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.Tools;
import com.ifcar.app.view.LoadingListView;
import com.ifcar.app.view.PullRefreshListView;
import com.ifcar.app.view.PullRefreshListView.OnRefreshListener;
import com.ifcar.app.activity.R;

import android.content.Context;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public abstract class NRAbsPullLoadingListFrament extends NRAbsLoadingListFrament {
    private PullRefreshListView mPullRefreshListView;

    private OnRefreshListener mOnRefreshListener;


    @Override
	public void onLoadingListViewAttached(LoadingListView loadingListView) {
		super.onLoadingListViewAttached(loadingListView);
		mPullRefreshListView = (PullRefreshListView) loadingListView.findViewById(R.id.list);
		mPullRefreshListView.setRefreshView(R.layout.refreshing_view);
        mPullRefreshListView.reset();

        if (mOnRefreshListener == null) {
            mOnRefreshListener = getDefaultOnRefreshListener(getActivity(), this);
        }

        mPullRefreshListView.setOnRefreshListener(getRefreshTag(), mOnRefreshListener);
	}

    @Override
    void setAdatper(LoadingListView loadingListView, ListAdapter adapter) {
        if (mPullRefreshListView instanceof GroupListView) {
            ((GroupListView)mPullRefreshListView).setAdapter((GroupAdapter)adapter);
        } else {
            super.setAdatper(loadingListView, adapter);
        }
        
    }



    @Override
	public void onLoadingListViewDetached(LoadingListView loadingListView) {
		super.onLoadingListViewDetached(loadingListView);
		if (mPullRefreshListView != null) {
			mPullRefreshListView = null;
		}
	}



	@Override
    protected void handleRefreshing(Object result) {
        if (mPullRefreshListView != null) {
            mPullRefreshListView.refreshDone();
        }
        super.handleRefreshing(result);
    }

    public PullRefreshListView getPullRefreshListView() {
        if (mPullRefreshListView != null) {
            return mPullRefreshListView;
        }
        return null;
    }

    public OnRefreshListener getDefaultOnRefreshListener(final Context context,
            final NRLoadingFrament loadingFrament) {
        return new OnRefreshListener() {

            @Override
            public void updateRefreshView(String tag, View view, int lastState, int state) {
                String key = tag;

                TextView indicatorText = (TextView) view.findViewById(R.id.indicator_text);
                View indicatorArrow = view.findViewById(R.id.indicator_arraw);
                View refreshPb = view.findViewById(R.id.refreshing_pb);

                if (lastState == PullRefreshListView.REFRESH_STATE_IDLE) {
                    TextView refreshTime = (TextView) view.findViewById(R.id.refresh_time);
                    refreshTime.setText(StringUtil.getRefreshTimeStr(context, key));
                }

                switch (state) {
                    case PullRefreshListView.REFRESH_STATE_IDLE:

                        break;
                    case PullRefreshListView.REFRESH_STATE_SHOWING:
                        indicatorArrow.setVisibility(View.VISIBLE);
                        refreshPb.setVisibility(View.INVISIBLE);
                        indicatorArrow.setEnabled(true);
                        if (lastState == PullRefreshListView.REFRESH_STATE_PREPARE_REFRESHING) {
                            indicatorArrow.startAnimation(AnimationUtils.loadAnimation(context,
                                    R.anim.pull_indicator_arrow));
                        }

                        indicatorText.setText(R.string.pull_down_refresh);

                        break;
                    case PullRefreshListView.REFRESH_STATE_PREPARE_REFRESHING:
                        indicatorArrow.setVisibility(View.VISIBLE);
                        refreshPb.setVisibility(View.INVISIBLE);
                        indicatorArrow.setEnabled(false);
                        if (lastState == PullRefreshListView.REFRESH_STATE_SHOWING) {
                            indicatorArrow.startAnimation(AnimationUtils.loadAnimation(context,
                                    R.anim.pull_indicator_arrow));
                        }
                        indicatorText.setText(R.string.relese_refresh);

                        break;
                    case PullRefreshListView.REFRESH_STATE_REFRESHING:
                        indicatorArrow.setVisibility(View.INVISIBLE);
                        refreshPb.setVisibility(View.VISIBLE);
                        indicatorArrow.clearAnimation();
                        indicatorText.setText(R.string.refreshing);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public boolean onPrepareRefresh(String tag) {
                if (!Tools.CheckNetwork(context)) {
                    Toast.makeText(context, R.string.msg_net_error, Toast.LENGTH_SHORT).show();
                    return false;
                }
                return true;
                // return false;
            }

            /**
             * 在下拉刷新时调用
             */
            @Override
            public void doRefresh(String tag) {
                loadingFrament.refresh();
            }
        };
    }
}
